package com.sk.weichat.ui.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.lock.ChangeDeviceLockPasswordActivity;
import com.sk.weichat.ui.lock.ResetDeviceLockActivity;
import com.sk.weichat.ui.lock.SetDeviceLockActivity;
import com.sk.weichat.ui.lock.DeviceLockHelper;
import com.sk.weichat.ui.lock.UnLockDeviceLockActivity;
import com.suke.widget.SwitchButton;

public class SecureSettingActivity extends BaseActivity {

    public static final int REQUEST_DISABLE_LOCK = 1231;
    private SwitchButton sbDeviceLock;
    //private SwitchButton sbDeviceLockFree;
    private View llDeviceLockDetail;
    private View rlChangeDeviceLockPassword;

    GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secure_setting);
        initActionBar();
        initView();

        gestureDetector = new GestureDetector(mContext, new GestureDetector.OnGestureListener() {

            @Override
            public boolean onDown(MotionEvent e) {

                return true;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                if (!sbDeviceLock.isChecked()) {
                    //设置密码
                    SetDeviceLockActivity.start(SecureSettingActivity.this);
                    return true;
                } else {
                    //解锁
                    UnLockDeviceLockActivity.start(SecureSettingActivity.this, REQUEST_DISABLE_LOCK, true);
                }
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {

            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                return false;
            }
        });
        sbDeviceLock.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
//
        rlChangeDeviceLockPassword.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ResetDeviceLockActivity.class);
            startActivity(intent);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateDeviceLockSettings();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }
        if (requestCode == REQUEST_DISABLE_LOCK) {
            //解锁成功
            DeviceLockHelper.clearPassword();
            updateDeviceLockSettings();
        }
    }

    private void updateDeviceLockSettings() {
        boolean enabled = DeviceLockHelper.isEnabled();
        sbDeviceLock.setChecked(enabled);
        if (enabled) {
            llDeviceLockDetail.setVisibility(View.VISIBLE);
        } else {
            llDeviceLockDetail.setVisibility(View.GONE);
        }
        // boolean autoLock = DeviceLockHelper.isAutoLock();
        //sbDeviceLockFree.setChecked(autoLock);
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tvTitle = (TextView) findViewById(R.id.tv_title_center);
        tvTitle.setText(R.string.setting_safety);
    }

    private void initView() {
        sbDeviceLock = findViewById(R.id.sbDeviceLock);
        //sbDeviceLockFree = findViewById(R.id.sbDeviceLockFree);
        llDeviceLockDetail = findViewById(R.id.llDeviceLockDetail);
        rlChangeDeviceLockPassword = findViewById(R.id.rlChangeDeviceLockPassword);
    }
}
